package org.company.management.utilities;

import java.util.ArrayList;
import java.util.List;

import org.company.management.entity.EmployeeSchedule;
import org.company.management.entity.EmployeeScheduleInfo;
import org.company.management.entity.SchedulerHour;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;

/**
 * @author Paner
 *
 */
public class FromToEditingSupport extends EditingSupport {

	private final TableViewer viewer;
	private int _columnIndex;
	

	public FromToEditingSupport(TableViewer viewer, int columnIndex) {
		super(viewer);
		this.viewer = viewer;
		_columnIndex = columnIndex;
	}

	@Override
	// selected cell type is text
	protected CellEditor getCellEditor(Object element) {
		return new TextCellEditor(viewer.getTable());
	}

	@Override
	protected boolean canEdit(Object element) {
		return true;
	}

	@Override
	protected Object getValue(Object element) {
		String employeeFromTo = "";
		EmployeeSchedule employeeShedule = (EmployeeSchedule) element;
		List<EmployeeScheduleInfo> tempList = new ArrayList<EmployeeScheduleInfo>();
		tempList = employeeShedule.getEmployeeScheduleInfoList();
		if(tempList != null && tempList.get(_columnIndex).getFromTo() != null)
		{
			employeeFromTo = tempList.get(_columnIndex).getFromTo().toString();
		}
			
		return employeeFromTo;

	}

	@Override
	protected void setValue(Object element, Object value) {
		EmployeeSchedule employeeShedule = (EmployeeSchedule) element;
		List<EmployeeScheduleInfo> tempList = new ArrayList<EmployeeScheduleInfo>();
		tempList = employeeShedule.getEmployeeScheduleInfoList();
		
		SchedulerHour hour = new SchedulerHour(String.valueOf(value));
		
		String fromTo = hour.getFormattedHour();

		if(!hour.getNeedsExtension())
		{
			if(tempList != null && tempList.get(_columnIndex) != null)
			{
				EmployeeScheduleInfo r = employeeShedule.getEmployeeScheduleInfoList().get(_columnIndex);
				r.setFromTo(fromTo);
				employeeShedule.getEmployeeScheduleInfoList().set(_columnIndex, r);
			}
		}
		else
		{
			if(tempList != null && tempList.get(_columnIndex) != null)
			{
				EmployeeScheduleInfo employeeScheduleInfo = employeeShedule.getEmployeeScheduleInfoList().get(_columnIndex);
				try
				{
					
					employeeScheduleInfo.setFromTo(hour.getFromHour()+ ":" + hour.getFromMinute() + "-" + "24:00");
					employeeShedule.getEmployeeScheduleInfoList().set(_columnIndex, employeeScheduleInfo);
				}
				catch(Exception ex)
				{
					
				}
				try
				{
					employeeScheduleInfo = employeeShedule.getEmployeeScheduleInfoList().get(_columnIndex+1);
					employeeScheduleInfo.setFromTo("00:01" + "-" + hour.getToHour() + ":" + hour.getToMinute());
					employeeShedule.getEmployeeScheduleInfoList().set(_columnIndex+1, employeeScheduleInfo);
				}
				catch(Exception ex)
				{
					
				}
				
			}
		
		}
		
		viewer.refresh();
	}
}